package torpedo_client.playerthreads;

import java.awt.Color;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import torpedo_client.Gui;
import torpedo_client.guicomponents.BattleSquare;
import torpedo_server.messages.*;

public class Connection extends Thread {

    private Socket soc;
    private int port;
    private String ip;
    private boolean CONNECTED = false, prevHIT = false;
    private BattleSquare bat = new BattleSquare();

    public Connection(String ip, int port) {
        try {
            soc = new Socket(ip, port);
            CONNECTED = true;
        } catch (UnknownHostException ex) {
            Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Connection() {
    }

    public Message getMessage() {
        ObjectInputStream ios = null;
        try {
            ios = new ObjectInputStream(this.soc.getInputStream());
        } catch (IOException ex) {
            Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
        }
        Message message = null;
        try {
            message = (Message) ios.readObject();
        } catch (IOException ex) {
            Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
        }

        return message;
    }

    public void sendMess(Message mess) throws IOException {
        OutputStream os = this.soc.getOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(os);
        oos.writeObject(mess);
        oos.flush();
    }

    @Override
    public void run() {
        try {
            sendMess(new WelcomeMessage("Hello!"));
        } catch (IOException ex) {
            System.out.println("Nem sikerült elküldeni a WelcomeMessaget");
        }
        while (!bat.testShips()) {
            try {
                Thread.sleep(5);
            } catch (InterruptedException ex) {
                Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        try {
            sendMess(new StartBattleMessage("Let the war begin!"));
        } catch (IOException ex) {
            System.out.println("Nem sikerült elküldeni a startBattleMessaget");
        }
        Gui.shipStatus.setText("Waiting for other player...");
        // --------------------------
        //message kezelő
        if (CONNECTED && BattleSquare.testShips()) {
            if ("MoveMessage".equals(getMessage().getType())) {
                Gui.shipStatus.setText("You turn! FIIRE!");
                //ide még kell a getHit elküldése

                //átgondolni geci----------------------------------------------------------------------------
                if (prevHIT && bat.shipReturn()) {
                    try {
                        sendMess(new HitMessage(bat.getPos(), true, true));
                    } catch (IOException ex) {
                        Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (prevHIT && !bat.shipReturn()) {
                    try {
                        sendMess(new HitMessage(bat.getPos(), true, false));
                    } catch (IOException ex) {
                        Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (!prevHIT) {
                    try {
                        sendMess(new HitMessage(bat.getPos(), false, true));
                    } catch (IOException ex) {
                        Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                //-------------------------------------------------------------------------------------------

            }
            if ("WaitMessage".equals(getMessage().getType())) {
                Gui.shipStatus.setText("Waiting for other player...");
                // kapott getHit kiérékelése + kirajzolása kell
                
                //átgondolni geci----------------------------------------------------------------------------
                if("HitMessage".equals(getMessage().getType())) {
                    HitMessage hitmsg;
                    hitmsg = (HitMessage) getMessage();
                    
                    for(int i = 0; i < BattleSquare.mainlist.size(); i++) {
                        if(BattleSquare.mainlist.get(i).getX() == hitmsg.getX() 
                                && BattleSquare.mainlist.get(i).getY() == hitmsg.getY()) {
                            BattleSquare.mainlist.get(i).setHit();
                            BattleSquare.mainlist.get(i).setBackground(Color.BLUE);
                            prevHIT = true;
                        } else {
                            prevHIT = false;
                        }
                    }
                    
                    if(!hitmsg.isLIVE()){
                        String message = "You Win! :)";
                        JOptionPane.showMessageDialog(null, message, "Game Over!", 1);
                    }
                }
                //-------------------------------------------------------------------------------------------
                
            }
            if ("EndMessage".equals(getMessage().getType())) {
                //ha nincs több hajóm és endmessaget katam akkor
                if (!bat.shipReturn()) {
                    Gui.shipStatus.setText("Game Over! You Lose! :(");
                    String message = "You Lose! :(";
                    JOptionPane.showMessageDialog(null, message, "Game Over!", 1);
                }
                //ha van még hajóm és endmesage akkor
                if (bat.shipReturn()) {
                    Gui.shipStatus.setText("You WIN!!! :)");
                    String message = "You Win! :)";
                    JOptionPane.showMessageDialog(null, message, "Game Over!", 1);
                }
            }
        }
    }
}